package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);
        Long user = BaseContext.getCurrentId();
//判断购物车是否存在该菜品或者套餐
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,user);
//        如果菜品id不为空，说明当前用户添加的是一个菜品，根据菜品id查询购物车
        queryWrapper.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
//        如果套餐id不为空，说明当前用户添加的是一个套餐，根据套餐id查询购物车
        queryWrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if(cartServiceOne!=null){
//            如果存在，数量加1 update
            cartServiceOne.setNumber(cartServiceOne.getNumber()+1);
           shoppingCartService.updateById(cartServiceOne);
           return R.success(cartServiceOne);
        }else{
//            如果不存在，添加购物车  数量默认1，insert
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(user);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R list(){
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R delete(){
       /* LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);*/
        shoppingCartService.clean();
        return R.success("清空购物车成功");
    }

    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        Long userId = BaseContext.getCurrentId();
        //判断购物车是否存在该菜品或者套餐
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        //        如果菜品id不为空，说明当前用户添加的是一个菜品，根据菜品id查询购物车
        queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
//        如果套餐id不为空，说明当前用户添加的是一个套餐，根据套餐id查询购物车
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        if (cartServiceOne != null) {
//            如果存在，数量-1 update
            cartServiceOne.setNumber(cartServiceOne.getNumber() - 1);
//            如果这个数量为0，则移除购物车
            if (cartServiceOne.getNumber() == 0) {
                shoppingCartService.remove(queryWrapper);
            }
            shoppingCartService.updateById(cartServiceOne);

        }
        return R.success(cartServiceOne);
    }

}
